using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using GrpcClient.Managers;

namespace GrpcClient.Views
{
    /// <summary>
    /// LoadingWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoadingWindow : Window
    {
        private bool IsLoad = false;
        //自动登录
        private bool AutoLogin = false;

        public LoadingWindow()
        {
            InitializeComponent();
        }

        private async void OnClickLogin(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(userName.Text))
            {
                MessageBox.Show("用户名不能为空");
                return;
            }
            if (string.IsNullOrEmpty(password.Password))
            {
                MessageBox.Show("密码不能为空");
                return;
            }

            await OnLoginAsync();
        }

        private async void OnClickRegister(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(userName.Text))
            {
                MessageBox.Show("用户名不能为空");
                return;
            }
            if (string.IsNullOrEmpty(password.Password))
            {
                MessageBox.Show("密码不能为空");
                return;
            }

            UpdateMessage("正在注册...");
            await UserManager.Instance.Register(userName.Text, password.Password);
            UpdateMessage("");
            if (UserManager.Instance.IsRegister)
            {
                RegisterCallback();
            }
        }

        private void LoginCallback()
        {
            MainWindow main = new MainWindow();
            Application.Current.MainWindow = main;
            Application.Current.MainWindow.Show();
            main.UserName = UserManager.Instance.CurrentUser?.Name ?? string.Empty;
            App.topThumb = new TopThumb();
            App.topThumb.Show();

            ConfigContainer.Instance.cacheManager.Set("username", userName.Text);
            ConfigContainer.Instance.cacheManager.Set("password", password.Password);
            IsLoad = false;
            this.Hide();
        }

        private void RegisterCallback()
        {
            MessageBox.Show("注册成功");
            userName.Text = "";
            password.Password = "";
        }

        private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private async Task OnLoginAsync()
        {
            UpdateMessage("正在登录...");
            await UserManager.Instance.Login(userName.Text, password.Password);
            UpdateMessage("");
            if (UserManager.Instance.IsLogin)
            {
                LoginCallback();
            }
        }

        public void UpdateMessage(string message)
        {
            if (string.IsNullOrEmpty(message))
                this.IsEnabled = true;
            else
                this.IsEnabled = false;

            this.message.Text = message;
        }


        protected override async void OnActivated(EventArgs e)
        {
            base.OnActivated(e);
            if (!IsLoad)
            {
                IsLoad = true;
                if (ConfigContainer.Instance.cacheManager.Get("username", out string username))
                {
                    userName.Text = username;
                }
                if (ConfigContainer.Instance.cacheManager.Get("password", out string psd))
                {
                    password.Password = psd;
                }

                register.Visibility = Visibility.Collapsed;
                if (ConfigContainer.Instance.cacheManager.Get("register", out bool isregister))
                {
                    if (isregister)
                    {
                        register.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        register.Visibility = Visibility.Collapsed;
                    }
                }

                if (AutoLogin)
                {
                    AutoLogin = false;
                    if (!string.IsNullOrEmpty(userName.Text) && !string.IsNullOrEmpty(password.Password))
                    {
                        await OnLoginAsync();
                    }
                }

            }

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (ConfigContainer.Instance.cacheManager.Get("username", out string username))
            {
                userName.Text = username;
            }
            if (ConfigContainer.Instance.cacheManager.Get("password", out string psd))
            {
                password.Password = psd;
            }

            register.Visibility = Visibility.Collapsed;
            if (ConfigContainer.Instance.cacheManager.Get("register", out bool isregister))
            {
                if (isregister)
                {
                    register.Visibility = Visibility.Visible;
                }
                else
                {
                    register.Visibility = Visibility.Collapsed;
                }
            }
        }
    }
}
